<%
layout("/layouts/platform.html"){
%>
<section class="content-wrap bg-white">
    <header class="header navbar bg-white shadow">
    <script language="JavaScript" type="text/javascript"
			src="${base}/assets/plugins/My97DatePicker/WdatePicker.js"></script>
			<br>
    <div class="btn-group tool-button">
			<div class="input-group date form_datetime layoutsSearch"
				style="width: 150px; float: left; padding-left: 5px; padding-right: 5px"
				data-date-format="dd MM yyyy" data-link-field="beginDate">
				<input class="form-control layoutsSearch" type='text' id="beginDate"
					name="beginDate" value='${today!}'
					onFocus="WdatePicker({maxDate:'#F{$dp.$D(\'endDate\')||\'%y-%M-%d\'}'})" />
			</div>
			<div class="input-group date form_datetime layoutsSearch"
				style="width: 150px; float: left; padding-left: 5px; padding-right: 5px"
				data-date-format="dd MM yyyy" data-link-field="endDate">
				<input class="form-control layoutsSearch" type='text' id='endDate' name='endDate'
					value='${today!}'
					onFocus="var date=limitMonthDate(2);WdatePicker({minDate:'#F{$dp.$D(\'beginDate\')||\'${month!}-01\'}',maxDate:date})" />
			</div>
		</div>
		<div class="btn-group tool-button" style="padding-left: 10px;">
			<div class="input-group"
				style="width: 200px; float: left; padding-left: 5px; padding-right: 5px">
				<span class="input-group-addon">订单状态</span> <select id="status"
					class="form-control layoutsSearch" onchange="search()">
					<option value="-1">全部</option>
					<option value="0">未发布</option>
					<option value="1">已发布</option>
					<option value="2">已确认（有货）</option>
					<option value="3">已确认（无货）</option>
					<option value="4">待揽件</option>
					<option value="5">已揽件</option>
					<option value="6">已关闭</option>
				</select>
			</div>
		</div>
		<div class="btn-group tool-button" id="taobao">
			<div class="input-group"
				style="width: 200px; float: left; padding-right: 5px">
				    <span class="input-group-addon">淘宝店主</span> <select id="name"
					class="form-control layoutsSearch" onchange="search()">
					<option value="-1">全部</option>
					<% for(items in obj){%>
					    <option value="${items.id}">${items.loginname}</option>
					<%}%>
				</select>
			</div>
		</div>
		<div class="btn-group tool-button" id="payfor">
			<div class="input-group"
				style="width: 200px; float: left; padding-right: 5px">
				<span class="input-group-addon">支付状态</span> <select id="pay"
					class="form-control layoutsSearch" onchange="search()">
					<option value="-1">全部</option>
					<option value="0">未支付</option>
					<option value="1">已支付</option>
				</select>
			</div>
		</div>
		<div class="btn-group tool-button" >
			<div class="input-group"
				style="width: 250px; float: left; padding-right: 5px">
				<span class="input-group-addon">电话号码</span> 
				<input class="form-control" type="text" id='mobilePhone' name='mobilePhone'/>
			</div>
		</div>
		<div class="btn-group tool-button" >
			<div class="input-group"
				style="width: 250px; float: left; padding-right: 5px">
				<span class="input-group-addon">收货人姓名</span> 
				<input class="form-control" type="text" id='recipient' name='recipient'/>
			</div>
		</div>
		<div class="btn-group tool-button" >
			<div class="input-group"
				style="width: 250px; float: left; padding-right: 5px">
				<span class="input-group-addon">买家会员名（旺旺号）</span> 
				<input class="form-control" type="text" id='account' name='account'/>
			</div>
		</div>
		<div class="btn-group tool-button" style="width:45px;">
			<button class="btn btn-primary navbar-btn" onclick="search()">查询</button>
		</div>
        <div class="pull-right offscreen-right">
            <button class="btn btn-primary navbar-btn" onclick="exportFile()" id="exportfile"><i
                    class="fa fa-sliders"></i> 导出
            </button>
            <!-- <button class="btn btn-primary navbar-btn" onclick="document.getElementById('xx').click();" id="file"><i
                    class="fa fa-sliders" ></i> 导入
            </button> -->
            <input type="file" name="xx" id="xx" value="" style="display: none"
				onchange="importFile(this)" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"/>
            
        </div>
    </header>
    <div class=panel-body>
        <div class="table-responsive no-border">
            <input id="unitid" type="hidden">
            <table class="table table-bordered table-striped mg-t datatable">
                <thead>
                <tr>
                        <th>日期</th>
						<th>买家会员名</th>
						<th>收货人姓名</th>
						<th>联系电话</th>
						<th>联系手机</th>
						<th>收货地址</th>
						<th>托寄物型号</th>
						<th>尺寸（长宽高）</th>
						<th>宝贝总数量</th>
						<th>颜色</th>
						<th>选择发货物流</th>
						<th>单号运费金额</th>
						<th>订单状态</th>
						<th>操作</th>
            </table>
        </div>
    </div>
</section>
<!-- 用户详情 -->
<div id="dialogUserDetail" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>
<div id="dialogUserRightDetail" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

        </div>
    </div>
</div>

<script language="JavaScript">
    var datatable;
    function initDatatable() {
        datatable = $('.datatable').DataTable({
            "dom": '<"toolbar">frtip',
            "searching":false,
            "processing": false,
            "serverSide": true,
            "select": true,
            "ordering": true,
            "language": {
                "url": "${base}/assets/plugins/datatables/cn.json"
            },
            "preDrawCallback": function () {
                sublime.showLoadingbar($(".main-content"));
            },
            "drawCallback": function () {
                sublime.closeLoadingbar($(".main-content"));
            },
            "ajax": {
                "url": "${base}/platform/losys/factory/order/data",
                "type": "post",
                "data": function (d) {
               	    d.beginDate = $("#beginDate").val();
                    d.endDate = $("#endDate").val();
                    d.status = $("#status").val(); 
                    d.name = $("#name").val(); 
                    d.pay = $("#pay").val(); 
                    d.mobilePhone = $("#mobilePhone").val();
                    d.recipient = $("#recipient").val();
                    d.account = $("#account").val();
               }
            },
            "order": [[0, "desc"]],
            "columns": [
                {"data": "orderdate", "bSortable": true},
                {"data": "account", "bSortable": true},
                {"data": "recipient", "bSortable": true},
                {"data": "fixedtelephone", "bSortable": true},
                {"data": "mobilephone", "bSortable": true},
                {"data": "address", "bSortable": true},
                {"data": "mailingmodel", "bSortable": true},
                {"data": "size", "bSortable": true},
                {"data": "quantity", "bSortable": true},
                {"data": "color", "bSortable": true},
                {"data": "logistics", "bSortable": true},
                {"data": "freight", "bSortable": true},
            ],
            "columnDefs": [
            	{
	                "render": function (data, type, row) {
	                    if (data) {
	                        return moment(parseInt(data*1000)).format("YYYY-MM-DD");
	                    }
	                    return '';
	                },
	                "targets": 0
	            },
	            {
	                "render": function (data, type, row) {
	                    if (row.orderstatus==1) {
	                        return "已发布";
	                    }else if(row.orderstatus==2){
	                    	return '已确认（有货）';
	                    }else if(row.orderstatus==3){
	                    	return '<span style="color:red;">已确认（无货）</span>';
	                    }else if(row.orderstatus==4){
	                    	return '待揽件';
	                    }else if(row.orderstatus==5){
	                    	return '已揽件';
	                    }else if(row.orderstatus==6){
	                    	return '已关闭';
	                    }
	                    return '未发布';
	                },
	                "targets": 12
	            },
	            {
	            	"render": function (data, type, row) {
	            		 var name="${@shiro.getPrincipalProperty('nickname')}";
	            		if(row.orderstatus==1 && name!="超级管理员"){
                    		return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
                            ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
                            '<li class="divider"></li>' +
                            '<li><a href="${base}/platform/losys/factory/order/confirm/' + row.orderid + '" data-toggle="modal" data-target="#homeDetail">确认订单</a></li>' +
                            '<li><a href="${base}/platform/losys/factory/order/detail/' + row.orderid + '" data-pjax>查看订单状态</a></li>' +
                            '</ul></div>';
	            		}else if(row.orderstatus==2 && name!="超级管理员"){
	            			return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
                            ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
                            '<li class="divider"></li>' +
                            '<li><a href="${base}/platform/losys/factory/order/parts/' + row.orderid + '" data-pjax>揽件</a></li>' +
                            '<li><a href="${base}/platform/losys/factory/order/detail/' + row.orderid + '" data-pjax>查看订单状态</a></li>' +
                            '</ul></div>';
	            		}else if(row.orderstatus==3 && name!="超级管理员"){
	            			return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
                            ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
                            '<li class="divider"></li>' +
                            '<li><a href="${base}/platform/losys/factory/order/confirm/' + row.orderid + '" data-toggle="modal" data-target="#homeDetail">状态变更</a></li>' +
                            '</ul></div>';
	            		}else{
	            			return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
                            ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
                            '<li class="divider"></li>' +
                            '<li><a href="${base}/platform/losys/factory/order/detail/' + row.orderid + '" data-pjax>查看订单状态</a></li>' +
                            '</ul></div>';
	            		}
                    },
	                "targets": 13
	            }
               
            ]
        });
        datatable.on('click', 'tr', function () {
            $(this).toggleClass('selected');
        });
        $("#searchBtn").on('click', function () {
            datatable.ajax.reload();
        });
    }
    //限制开始和结束时间为同一个月
    function limitMonthDate(e) {
        var DateString;
        if (e == 2) {
            var beginDate = $dp.$("beginDate").value;
            if (beginDate != "" && beginDate != null) {
                var limitDate = new Date(beginDate);
                limitDate.setDate(new Date(limitDate.getFullYear(), limitDate
                        .getMonth() + 1, 0).getDate()); //获取此月份的天数
                DateString = limitDate.getFullYear() + '-'
                    + (limitDate.getMonth() + 1) + '-'
                    + limitDate.getDate();
                return DateString;
            }
        }
        if (e == 1) {
            var endDate = $dp.$("endDate").value;
            if (endDate != "" && endDate != null) {
                var limitDate = new Date(endDate);
                limitDate.setDate("1"); //设置闲置时间为月初
                DateString = limitDate.getFullYear() + '-'
                    + (limitDate.getMonth() + 1) + '-'
                    + limitDate.getDate();
                return DateString;
            }
        }

    }
    function search() {
        datatable.ajax.reload();
    }
    function exportFile(){
    	var phone=-1,account=-1,recipient=-1;
    	if($("#mobilePhone").val()!=""){
    		phone=$("#mobilePhone").val();
    	}
    	if($("#recipient").val()!=""){
    		recipient=$("#recipient").val();
    	}
    	if($("#account").val()!=""){
    		account=$("#account").val();
    	}
    	window.location.href="${base}/platform/losys/factory/order/exportFile/"+$("#beginDate").val()+"/"+$("#endDate").val()+"/"+$("#status").val()+"/"+$("#name").val()+"/"+$("#pay").val()+"/"+phone+"/"+recipient+"/"+account;
    }
    function importFile(e) {
    	if (!window.File && !window.FileList && !window.FileReader && !window.Blob) {
    		document.write('您的浏览器不支持File Api');
    	}
    	e = e || window.event;
    	var liId = $(e.target).attr("id");
    	var suffix = $("#xx")[0].files[0].name.split(".")[1]
        if(suffix != 'xls' && suffix !='xlsx'){
        	Toast.error("请导入后缀为xlsx,xls的文件!");
            return;
        }
    	var fd = new FormData();
    	fd.append("file", $("#xx")[0].files[0]);
        var fileurl ="${base}/platform/losys/factory/order/importFile";
    	$.ajax({
    		type : "POST",
    		contentType : false, // 必须false才会避开jQuery对 formdata 的默认处理 ,
    								// XMLHttpRequest会对 formdata 进行正确的处理
    		processData : false, // 必须false才会自动加上正确的Content-Type
            url : fileurl,
    		data : fd,
    		success : function(data) {
    			console.log(data);
    			Toast.success(data.msg);
    			datatable.ajax.reload(null,false);
    		},
    		error : function(msg) {
    			console.log(msg);
    		}
    	});
    }
    $(document).ready(function () {
        initDatatable();
        $('#homeDetail').on('hidden.bs.modal',function(){
            $(this).removeData("bs.modal");
        });
        var name="${@shiro.getPrincipalProperty('nickname')}";
        if(name=="超级管理员"){
        	$("#file").hide();
        	$(".panel-body").css("padding-top","100px");
        }else{
        	$(".panel-body").css("padding-top","70px");
        	$("#taobao").hide();
        	$("#payfor").hide();
        }
    });

</script>
<%}%>
